'use strict';

const Service = require('egg').Service;

module.exports = class SettingService extends Service {
  // Update Global Settings ///////////////////////////////////////////////////////////////////
  async updateSettings() {
    const func = this.ctx.helper;
    const input = this.ctx.request.body;
    const connBlog = this.ctx.app.connBlog;
    const theCache = this.ctx.app.theCache;

    // Check path
    if (input.baseurl) {
      input.baseurl.replace(/\\/g, '/');
      if (input.baseurl.slice(-1) != '/') input.baseurl += '/';
    }
    if (input.logoimage) {
      input.logoimage.replace(/\\/g, '/');
      if (input.logoimage.slice(-1) == '/') input.logoimage = input.stylesheet.substr(0, input.stylesheet.length - 1);
    }
    if (input.stylesheet) {
      input.stylesheet.replace(/\\/g, '/');
      if (input.stylesheet.slice(-1) == '/') input.stylesheet = input.stylesheet.substr(0, input.stylesheet.length - 1);
    }
    if (input.imagefolder) {
      input.imagefolder.replace(/\\/g, '/');
      if (input.imagefolder.slice(-1) == '/') input.imagefolder = input.imagefolder.substr(0, input.imagefolder.length - 1);
    }
    if (input.smiliesfolder) {
      input.smiliesfolder.replace(/\\/g, '/');
      if (input.smiliesfolder.slice(-1) == '/') input.smiliesfolder = input.smiliesfolder.substr(0, input.smiliesfolder.length - 1);
    }
    if (input.uploadpath) {
      input.uploadpath.replace(/\\/g, '/');
      if (input.uploadpath.slice(-1) != '/') input.uploadpath += '/';
    }
    // Do updates
    await connBlog.exec(genStrUpdate('blogtitle'));
    await connBlog.exec(genStrUpdate('blogdescription'));
    await connBlog.exec(genStrUpdate('baseurl'));
    await connBlog.exec(genStrUpdate('logoimage'));
    await connBlog.exec(genStrUpdate('blogwebmaster'));
    await connBlog.exec(genStrUpdate('blogwebmasteremail'));
    await connBlog.exec(genStrUpdate('bloglanguage'));
    await connBlog.exec(genBoolUpdate('defaultviewmode'));
    await connBlog.exec(genIntUpdate('articleperpagenormal', 0, 21));
    await connBlog.exec(genIntUpdate('articleperpagelist', 19, 101));
    await connBlog.exec(genIntUpdate('listentryperpage', 9, 101));
    await connBlog.exec(genIntUpdate('commentperpage', -1, 51));
    await connBlog.exec(genBoolUpdate('commenttimeorder'));
    await connBlog.exec(genBoolUpdate('showtrackbackwithcomment'));
    await connBlog.exec(genIntUpdate('showtrackbackposition', -1, 3));
    await connBlog.exec(genIntUpdate('recentarticlelist', 2, 31));
    await connBlog.exec(genIntUpdate('recentcommentlist', 2, 31));
    await connBlog.exec(genBoolUpdate('enabledynamiccalendar'));
    await connBlog.exec(genBoolUpdate('enablecontentautosplit'));
    await connBlog.exec(genIntUpdate('contentautosplitchars', 99, 2001));
    await connBlog.exec(genBoolUpdate('enableregister'));
    await connBlog.exec(genBoolUpdate('enablesecuritycode'));
    await connBlog.exec(genBoolUpdate('enabletrackbackin'));
    await connBlog.exec(genBoolUpdate('enabletrackbackout'));
    await connBlog.exec(genBoolUpdate('enablecomment'));
    await connBlog.exec(genIntUpdate('maxcommentlength', 99, 2001));
    await connBlog.exec(genIntUpdate('maxcommenturlcount', -1, 11));
    await connBlog.exec(genBoolUpdate('enableguestbook'));
    await connBlog.exec(genIntUpdate('entryperpageguestbook', 2, 31));
    await connBlog.exec(genStrUpdate('stylesheet'));
    await connBlog.exec(genStrUpdate('imagefolder'));
    await connBlog.exec(genStrUpdate('smiliesfolder'));
    await connBlog.exec(genIntUpdate('smiliesperrow', 0, 31));
    await connBlog.exec(genIntUpdate('minpostduration', 0, 601));
    await connBlog.exec(genBoolUpdate('enablevisitorrecord'));
    await connBlog.exec(genIntUpdate('maxvisitorrecord', 0, 301));
    await connBlog.exec(genBoolUpdate('enableupload'));
    await connBlog.exec(genIntUpdate('uploadsize', 1023, 50000001));
    await connBlog.exec(genStrUpdate('uploadpath'));
    await connBlog.exec(genStrUpdate('uploadtypes'));

    await theCache.loadCalendar();
    await theCache.loadArticles();
    await theCache.loadComments();

    function genStrUpdate(strName) {
      if (input[strName]) {
        return "UPDATE blog_settings SET set_value1='" + func.checkStr(func.trim(input[strName])) + "' WHERE set_name='" + strName + "';";
      }
      return '';

    }

    function genBoolUpdate(strName) {
      input[strName] = input[strName] ? 1 : 0;
      return 'UPDATE blog_settings SET set_value0=' + input[strName] + " WHERE set_name='" + strName + "';";
    }

    function genIntUpdate(strName, intMin, intMax) {
      input[strName] = func.checkInt(input[strName]);
      if (input[strName] > intMin && input[strName] < intMax) {
        return 'UPDATE blog_settings SET set_value0=' + input[strName] + " WHERE set_name='" + strName + "';";
      }
      return '';

    }

  }
};
